//https://github.com/api/v2/json/repos/show/oscargodson
var siteSettings = {
  
}

//Selector cache for frequently used selectors
var s = {
  content: $('#content')
};

/**
 * parseTemplate takes a string and a JS Object and returns a string with template
 * replaced with content you provided. Template tags look like: {{tag}}
 * @param {String} s This is the string to search for tags in.
 * @param {Object} j This is the JS object that contains a key=>value pair for the tag and what to be replaced
 * @returns {String} returns the modified string with the content you provided in replacement of the tags
 * @example var html = parseTemplate('Hey, {{name}}',{ name:'John' }); //returns "Hey, John"
 */
var parseTemplate = function(s,j){
  for(x in j){ var r = new RegExp('{{'+x+'}}','g'); s = s.replace(r,j[x]); }
  return s;
}

var routeURL = function(){
  
  var isPage = function(url){
    if(window.location.hash.indexOf(url) > -1){ return true; }
    else{ return false; }
  }
  
  var changePage = function(c){
    s.content.fadeOut(150,function(){
      s.content.empty().append(c).fadeIn(150);
    });
  }
  
  //If the value is #!/home or just #!/ or no hash, go home
  if(window.location.hash.length < 4 || isPage('/writing/')){
    changePage(s.content.data('home-content'));
    _gaq.push(['_trackEvent', 'Pages', 'View', 'Home']);
  }
  
  //If the page is of an article type...
  if(isPage('/article/')){
    var json = s.content.data(window.location.href.split('/article/')[1]);
    s.content.fadeOut(150,function(){
      var newHTML = parseTemplate($('[name="single-article"]').html(),{
        id:json.id,
        title:json.title,
        content:json.body_html.replace('<div class="posterous_more">POSTEROUS_MORE</div>','')
      });
      s.content.append(newHTML);
      $(this).empty().append(newHTML).fadeIn(150);
      _gaq.push(['_trackEvent', 'Pages', 'View', json.title]);
    });
  }
  
  //If it's the about page:
  if(isPage('/about/')){
    changePage($('#about').html());
    _gaq.push(['_trackEvent', 'Pages', 'View', 'About']);
  }
  
  //If it's the about page:
  if(isPage('/contact/')){
    changePage($('#contact').html());
    _gaq.push(['_trackEvent', 'Pages', 'View', 'Contact']);
  }
  
  if(isPage('/code/')){
    if(s.content.data('code-list') == undefined){
      $.getJSON('https://github.com/api/v2/json/repos/show/oscargodson?callback=?',function(json){
        var repos = json.repositories
        ,   newHTML = '';
        repos.reverse();
        for(x in repos){
          if(repos[x].fork == false && repos[x].size > 0){
            newHTML = newHTML+parseTemplate($('[name="code-listing"]').html(),{
              name:repos[x].name.replace(/--/g,'-').replace(/-/g,' '),
              description:repos[x].description,
              url:repos[x].url
            });
          }
        }
        var newHTML = parseTemplate($('[name="code-page"]').html(),{ list:newHTML });
        s.content.data('code-list',newHTML);
        changePage(s.content.data('code-list'));
      });
    }
    else{
      changePage(s.content.data('code-list'));
    }
    _gaq.push(['_trackEvent', 'Pages', 'View', 'Code']);
  }
  
}

//Loads the initial home page
$.getJSON('http://posterous.com/api/2/users/41481/sites/105548/posts/public?callback=?',function(json){
  for(x in json){
    //Here we store the full JSON for each article for instant loading!
    s.content.data(JSON.stringify(json[x].id),json[x]);
    var newHTML = parseTemplate($('[name="home-articles"]').html(),{
      id:json[x].id,
      title:json[x].title,
      content:json[x].body_html.split('<p><!--more--></p>')[0].split('<div class="posterous_more">POSTEROUS_MORE</div>')[0]
    });
    s.content.append(newHTML);
  }
  s.content.data('home-content',s.content.html());
  
  routeURL();
});

window.onhashchange = function(){ routeURL(); }

$('a[href^="http"]').prepend('<img title="Open link in new tab" class="external-link" src="images/link.png"> ');
$('body').delegate('.external-link','click',function(){
  window.open($(this).parent().attr('href'));
  return false;
});